package window;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import main.DataConnection;
import window.MainWindow.ProblemControls;


public class ProblemsSideBar extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final MainWindow mainW;
	private JPanel contentPane;
	private ArrayList<SideBarItem> probList = new ArrayList<SideBarItem>();
	private final Point startPoint = new Point(10, 10);
	private final int btnWidht = 100;
	private final int btnHeight = 30;
	private int sideBarsCount = 0;
	
	ProblemsSideBar(MainWindow mw){
		this.mainW = mw;
		setResizable(true);
		setTitle("Problems Sidebar");
		setBounds(10, 100, 250, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JButton btnChange = new JButton("Filter");
		btnChange.setBounds(startPoint.x, startPoint.y, btnWidht, btnHeight);
		getContentPane().add(btnChange);
		
		// updates MainWindow controls
		btnChange.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<String> probToUpdate = new ArrayList<String>();
				for(SideBarItem sbi : probList){
					if(sbi.isChecked()){
						probToUpdate.add(sbi.getName());
					}
				}
				mainW.updateProblemControls(probToUpdate);
				System.out.println("Change button clicked");
			}
		});	
		
		JButton btnRefresh = new JButton("Refresh DB");
		btnRefresh.setBounds(startPoint.x+btnWidht+10, startPoint.y, btnWidht, btnHeight);
		getContentPane().add(btnRefresh);
		
		btnRefresh.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {			
				removeSBItems();
				initSideBar(mainW.getProblCtrls());
				getContentPane().repaint();
			}

			private void removeSBItems() {
				for (SideBarItem sbi : probList)
					sbi.clear();
				probList.clear();
			}
		
		});	
		
		initSideBar(mainW.getProblCtrls());
	
	}

	private void initSideBar(ArrayList<ProblemControls> problCtrls) {
		String[] probNames = DataConnection.getProblemNames();
		for(String probName : probNames){
			addProblem(probName);			
		}
		setChecks(problCtrls);
	}

	private void addProblem(String probName) {
		probList.add(new SideBarItem(probName));
	}
	
	private void setChecks(ArrayList<ProblemControls> problCtrls){
		Iterator<SideBarItem> it;
		for(ProblemControls pc : problCtrls){
			it = probList.iterator();
			while(it.hasNext()){
				SideBarItem sbi = it.next();
				if(sbi.getName().equalsIgnoreCase(pc.getName())){
					sbi.setChecked(true);
					break;
				}
			}		
		}
	}

	public class SideBarItem{
		private JCheckBox chkBox;
		private final int chkBoxWidth = 200;
		private final int chkBoxHeight = 14;
		private final int chkVPadding = 10;
		
		public SideBarItem(String probName){
			chkBox = addCheckBox(probName);
			
			getContentPane().add(chkBox);
		}

		private JCheckBox addCheckBox(String probName) {
			JCheckBox jcb = new JCheckBox(probName);
			jcb.setToolTipText(probName);
			jcb.setBounds(startPoint.x, getValueY(), chkBoxWidth, chkBoxHeight);
			return jcb;
		}

		private int getValueY() {
			return startPoint.y+chkVPadding+btnHeight*(++sideBarsCount);
		}
		
		public boolean isChecked(){
			return chkBox.isSelected();
		}
		
		public void setChecked(boolean bool){
			chkBox.setSelected(bool);
		}
		
		public String getName(){
			return chkBox.getText();
		}
		
		public JCheckBox getCheckBox(){
			return chkBox;
		}
		
		public void clear(){
			sideBarsCount--;
			getContentPane().remove(chkBox);
		}
		
		public boolean equals(Object obj){
			SideBarItem sbi = (SideBarItem) obj;
			return this.getName().equalsIgnoreCase(sbi.getName());		
		}

	}
}
